/**
 * PLM项目资源服务
 * 提供资源相关的服务功能
 */

// 资源类型映射
const RESOURCE_TYPES = {
  DOCUMENT: 1,
  IMAGE: 2,
  VIDEO: 3,
  AUDIO: 4,
  OTHER: 99
};

// 资源服务
const resourceService = {
  /**
   * 获取资源列表
   * @param {Object} params 查询参数
   * @returns {Promise<Array>} 资源列表
   */
  getResourceList: async function(params = {}) {
    console.log('调用resourceService.getResourceList', params);
    
    // 返回模拟数据
    return {
      code: 0,
      data: {
        list: [
          {
            _id: 'res001',
            name: '需求文档.docx',
            type: RESOURCE_TYPES.DOCUMENT,
            size: 1024 * 10,
            url: 'https://example.com/docs/req.docx',
            createTime: Date.now() - 86400000 * 3
          },
          {
            _id: 'res002',
            name: '概要设计.pdf',
            type: RESOURCE_TYPES.DOCUMENT,
            size: 1024 * 50,
            url: 'https://example.com/docs/design.pdf',
            createTime: Date.now() - 86400000 * 2
          }
        ],
        total: 2
      }
    };
  },
  
  /**
   * 获取资源详情
   * @param {string} id 资源ID
   * @returns {Promise<Object>} 资源详情
   */
  getResourceDetail: async function(id) {
    console.log('调用resourceService.getResourceDetail', id);
    
    // 返回模拟数据
    return {
      code: 0,
      data: {
        _id: id,
        name: '模拟资源' + id,
        type: RESOURCE_TYPES.DOCUMENT,
        size: 1024 * 30,
        url: 'https://example.com/resources/' + id,
        createTime: Date.now() - 86400000,
        updateTime: Date.now(),
        description: '这是一个模拟资源，用于测试',
        tags: ['文档', '设计', '测试']
      }
    };
  },
  
  /**
   * 资源格式化
   * @param {Object} resource 资源对象
   * @returns {Object} 格式化后的资源对象
   */
  formatResource: function(resource) {
    if (!resource) return null;
    
    // 计算文件大小显示
    let sizeDisplay = '';
    const size = resource.size || 0;
    
    if (size < 1024) {
      sizeDisplay = size + 'B';
    } else if (size < 1024 * 1024) {
      sizeDisplay = (size / 1024).toFixed(2) + 'KB';
    } else {
      sizeDisplay = (size / (1024 * 1024)).toFixed(2) + 'MB';
    }
    
    // 获取文件类型显示
    let typeDisplay = '未知';
    switch (resource.type) {
      case RESOURCE_TYPES.DOCUMENT:
        typeDisplay = '文档';
        break;
      case RESOURCE_TYPES.IMAGE:
        typeDisplay = '图片';
        break;
      case RESOURCE_TYPES.VIDEO:
        typeDisplay = '视频';
        break;
      case RESOURCE_TYPES.AUDIO:
        typeDisplay = '音频';
        break;
      default:
        typeDisplay = '其他';
    }
    
    // 返回格式化后的资源
    return {
      ...resource,
      sizeDisplay,
      typeDisplay,
      createTimeDisplay: new Date(resource.createTime).toLocaleString()
    };
  }
};

// 导出资源服务
module.exports = resourceService; 